export default {
  data() {
    return {
      categoryLevelOne: [],
      categoryLevelTwo: []
    }
  },
  methods: {
    async getGoodsCategoryList(callback) {
      const { data } = await this.$http.goods.classify.getClassifySelect()
      // 一级分类
      this.initOneLevel(data)
      callback && callback()
      // 二级分类
      this.initTwoLevel(data)
    },
    // 初始化一级分类
    initOneLevel(data) {
      this.categoryLevelOne = data.map(item => {
        return {
          value: item.id,
          label: item.firstName
        }
      })
    },
    // 初始化二级分类
    initTwoLevel(data) {
      this.categoryLevelTwo = data.map(item => {
        return {
          value: item.id,
          categoryTwoList: item.childList?.map(item => {
            return {
              value: item.id,
              label: item.secondName
            }
          })
        }
      })
    },
    // 根据一级分类id查二级分类
    getCategoryTwoLevel(value) {
      return this.categoryLevelTwo.find(item => item.value === value)?.categoryTwoList
    },
    // 获取一级分类label
    getOneLevelLabel(value) {
      return this.categoryLevelOne.find(item => item.value === value)?.label
    },
    // 获取二级分类label
    getTwoLevelLabel(oneValue, twoValue) {
      return this.categoryLevelTwo
        .find(item => item.value === oneValue)
        ?.categoryTwoList.find(item => item.value === twoValue)?.label
    }
  }
}
